package com.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.ClassesService;
import com.hzit.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
	@Autowired
	private ClassesService classesService;
	//1. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pageSize, Student student) {
		//1.1 定义LambdaQueryWrapper （lamba查询包装器）
		LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
		//1.2 处理查询参数并构造包装器对象
		if(student != null){
			wrapper.like(StrUtil.isNotBlank(student.getSname()),Student::getSname,student.getSname())
					.like(StrUtil.isNotBlank(student.getAddr()),Student::getAddr,student.getAddr())
					.eq(student.getCid()!=null && student.getCid() != 0,Student::getCid,student.getCid());
		}
		//1.3 开始分页查询
		Page<Student> studentPage = this.page(new Page<>(page, pageSize), wrapper);
		//1.4 处理学生所在的班级名称
		List<Student> records = studentPage.getRecords();
		for (Student record : records) {
			Classes classes = classesService.getById(record.getCid());
			record.setCname(classes.getCname());
		}
		//1.5 定义PageReseult对象，并返回
		return new PageResult<>(studentPage.getTotal(),records);
	}
}
